package com.toune.villager.activity

import android.graphics.Paint
import android.os.Bundle
import android.text.InputFilter
import android.text.Spanned
import android.view.View
import android.widget.RadioButton
import android.widget.RadioGroup
import com.toune.villager.R
import com.toune.villager.adapter.MyBannerAdapter
import com.toune.villager.bean.BannerDataBean
import com.toune.villager.model.ShopDetailModel
import com.toune.dltools.DLSnackbarUtils
import com.toune.dltools.ui.DLBaseActivity
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.activity_shop_detail.*

class ShopDetailActivity : DLBaseActivity<ShopDetailModel>() {
    override val layout: Int
        get() = R.layout.activity_shop_detail
    override val titleStr: String?
        get() = "商品详情"

    var chiCunDatas = ArrayList<String>()
    override fun init(savedInstanceState: Bundle?) {
        useBanner()
        oldPriceTv.paintFlags = oldPriceTv.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
        setChiCunView()
    }

    private fun setChiCunView() {
        for (chiCunData in 0..5) {
            val rb = View.inflate(context, R.layout.layout_chicun_rb, null) as RadioButton
            rb.text = "ss$chiCunData"
            var lp = RadioGroup.LayoutParams(
                RadioGroup.LayoutParams.WRAP_CONTENT,
                RadioGroup.LayoutParams.WRAP_CONTENT
            )
            rb.tag = chiCunData.toString()
            rb.id = chiCunData
            lp.setMargins(10, 5, 10, 5)
            chiCunRp.addView(rb, lp)
        }
        chiCunRp.setOnCheckedChangeListener { group, checkedId ->

        }
    }

    /** 输入框小数的位数 */
    private val DECIMAL_DIGITS = 1

    /**
     * 设置小数位数控制
     */
    var lengthfilter: InputFilter = object : InputFilter {
        override fun filter(
            source: CharSequence, start: Int, end: Int,
            dest: Spanned, dstart: Int, dend: Int
        ): CharSequence? {
            // 删除等特殊字符，直接返回
            if ("" == source.toString()) {
                return null
            }
            val dValue: String = dest.toString()
            val splitArray = dValue.split("\\.".toRegex()).toTypedArray()
            if (splitArray.size > 1) {
                val dotValue = splitArray[1]
                val diff = dotValue.length + 1 - DECIMAL_DIGITS
                if (diff > 0) {
                    return source.subSequence(start, end - diff)
                }
            }
            return null
        }
    }

    override fun initEventAndData() {
        numberEt.filters = arrayOf(lengthfilter)
        numberEt.isCursorVisible = false;// 隐藏光标
        cutBtn.setOnClickListener {
            val toDouble = numberEt.text.toString().toInt()
            if (toDouble > 1) {
                numberEt.setText("${toDouble - 1}")
            }
        }
        plusBtn.setOnClickListener {
            val toDouble = numberEt.text.toString().toInt()
            numberEt.setText("${toDouble + 1}")
        }

        buyBtn.setOnClickListener {
            DLSnackbarUtils.Short(buyBtn,"买买买！！！").show()
        }
    }

    val imgUrls = arrayListOf(
        "https://pic1.shejiben.com/case/2015/05/06/20150506152119-e4585806.jpg",
        "https://pic.shejiben.com/case/2016/03/14/20160314114629-50dd4c89.jpg",
        "https://tse2-mm.cn.bing.net/th/id/OIP-C.xJd9T26hKLpRSLhgqy75bwAAAA?pid=ImgDet&rs=1",
        "https://pic1.shejiben.com/case/2014/08/15/20140815142317-cfec0131.jpg"
    )

    fun useBanner() { //--------------------------简单使用-------------------------------
        val bannerDatas: MutableList<BannerDataBean> = ArrayList()
        for (imgUrl in imgUrls) {
            var bannerDataBean = BannerDataBean()
            bannerDataBean.imgUrl = imgUrl
            bannerDatas.add(bannerDataBean)
        }

        banner.addBannerLifecycleObserver(this) //添加生命周期观察者
            .setAdapter(MyBannerAdapter(context, bannerDatas)).indicator = CircleIndicator(context)
    }
}